<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ include file="../common/header.jsp" %>
<style>
    body {
        background-color: #f0f0f0;
    }
</style>
    <div class="row">
        <div class="col-sm-12">

            <div style="width: 100%; position: relative; background: #ffffff; border: 1px solid #cccccc; margin: 80px 0; padding: 40px 15px;">
                <div class="log-icon"></div>
                <h2 class="text-center" style="line-height: 60px; font-size: 24px;">Accounting Change Readiness</h2>

                <div class="row">
                    <div class="col-lg-10 col-md-offset-1 mt10 question" id="question-js">
                        <p>Title: <span id="title-js"></span></p>
                        <h3>Result: <span id="result-js"></span></h3>
                        <p>Please make sure you fill out the answer.</p>
                        <div id="confirm-js">

                        </div>
                        <button id="next-js" style="" type="button" class="btn btn-primary btn-lg center-block pull-left">
                            submit
                        </button>
                        <a href="javascript:history.go(-1)" style="width: 150px; margin-left: 30px" type="button" class="btn btn-default  btn-lg center-block pull-left">
                            previous
                        </a>

                    </div>
                </div>

            </div>
        </div>

    </div>

<%@ include file="../common/footer.jsp" %>

<script>
    $(function(){
        var object = [], data= {};
        var answer = $.parseJSON(window.localStorage.getItem('questionAnswerData'));

        var questionTitle =  $.parseJSON(window.localStorage.getItem('questionTitleData'));
        var language = window.localStorage.getItem('language');

        $('#title-js').html(answer.title);
		
        $.get('html/answerConfirm/confirm.html',function(html){
            var tml =_.template(html);
            $('#confirm-js').html(tml({answer:answer, questionTitle:questionTitle, language:language}));
            if(result.sectionOneResult){//IFRS16部分 结果
            	var tem = $('#sectiononeresult');
                if(result.sectionOneResult == 'LOW'){
                	tem.addClass('green-font');
                }else if(result.sectionOneResult == 'Moderate'){
                	tem.addClass('orange-font');
                }else {
                	tem.addClass('red-font');
                }

                tem.html(result.sectionOneResult);
            }
            $('#next-js').click(function(){
                var data = answer;
                data.results = results().result;   //得到结果
                data.sectionOneResult = results().sectionOneResult;   //得到结果
                data.sectionTwoResult = results().sectionTwoResult;   //得到结果
                data.sectionThreeResult = results().sectionThreeResult;   //得到结果
               // var reqData = {"answer": data, "questions": questionTitle};
              //  var now = new Date();
              //  data.createDate = (now.getYear() +  '-' + now.getMonth() + '-' + now.getDate()).toString();
                $.ajax({
                    url:'/questionnaire/answer/addItemPost',
                    type:'post',
                    dataType:'json',
                    data: data,
                    success:function(dataSucc){
                        window.localStorage.setItem('answerResults',data.results);
                        window.location.href='/questionnaire/results';
                    }
                });
            });
        });
        
        var result = results();   //得到答案

        if(result.result){//总的 结果
            if(result.result == 'LOW'){
                $('#result-js').addClass('green-font');
            }else if(result.result == 'Moderate'){
                $('#result-js').addClass('orange-font');
            }else {
                $('#result-js').addClass('red-font');
            }

            $('#result-js').html(result.result);
        }
        

//        算出结果
        function results(){
            var resArr = answer.content.split(',');
            var highArr = [];
            var highSectionOneArr = [];
            var highSectionTwoArr = [];
            var highSectionThreeArr = [];
            var score = 0;
           // var results = 'Low';
           var results = {
        		   result:'Low', //总的结果
        		   sectionOneResult: 'Low',  //IFRS16 部分  结果
        		   sectionTwoResult: 'Low',  //IFRS15部分  结果
        		   sectionThreeResult: 'Low' //IFRS9部分  结果
           }
			if(questionTitle[1].impact == "High"||questionTitle[4].impact == "High"||
			   questionTitle[6].impact == "High"||questionTitle[9].impact == "High"||
			   questionTitle[10].impact == "High"){
				results.result = 'High';
				results.sectionOneResult = 'High';
			}
           if(questionTitle[12].impact == "High"){
        	   results.result = 'High';
			   results.sectionTwoResult = 'High';
           }
           
           $.each(resArr,function(i,item){
        	 if(0<=i<11){
               if(item == 'true'){   //客户回答yes
                   if(questionTitle[i].impact == 'High'){
                       highArr.push(questionTitle[i].id);
                       highSectionOneArr.push(questionTitle[i].id);
                   }
               }
        	 }
        	 
        	 if(11<=i<14){
                 if(item == 'true'){   //客户回答yes
                     if(questionTitle[i].impact == 'High'){
                         highArr.push(questionTitle[i].id);
                         highSectionTwoArr.push(questionTitle[i].id);
                     }
                 }
          	 }
        	 
        	 
        	 if(i=14){
                 if(item == 'true'){   //客户回答yes
                     if(questionTitle[i].impact == 'High'){
                         highArr.push(questionTitle[i].id);
                         highSectionThreeArr.push(questionTitle[i].id);
                     }
                 }
          	 }
           });
           
           		//总的部分
           	if(results.result != 'High'){
                  if(highArr.length>=4){
                      results.result = 'High';
                  }else if(3 >=highArr.length && highArr.length>=1){
                     results = 'Moderate';
                  }else{
                      results = 'Low';
                 }
           	}   
                  //第一部分
			if(results.sectionOneResult != 'High'){                  
           		  if(highSectionOneArr.lengh>=4){
           	        results.sectionOneResult = 'High';
                  }else if(3 >=highArr.length && highArr.length>=1){
                     results.sectionOneResult = 'Moderate';
                  }else{
                      results.sectionThreeResult = 'Low';
                 }  
			}
      //      $.each(resArr,function(i,item){
      //          if(item == 'true'){   //客户回答yes
      //              score++            //分数
      //              if(questionTitle[i].impact == 'High'){
      //                  highArr.push(questionTitle[i].id);
      //              }
      //          }
      //      });

     //       if(highArr.length>=4){
     //           results = 'High';
     //       }else if(3 >=highArr.length && highArr.length>=1){
     //          results = 'Moderate';
     //       }else{
     //           results = 'Low';
     //      }
            return results;
        }
    });

</script>